package com.ocm.commerceinteraction.ui;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ocm.commerceinteraction.GlobalApplication;
import com.ocm.commerceinteraction.R;
import com.ocm.commerceinteraction.config.RequestAddr;
import com.ocm.commerceinteraction.util.MethedUtil;
import com.ocm.commerceinteraction.util.helper.LogHelper;
import com.ocm.commerceinteraction.util.http.AjaxCallBack;
import com.ocm.commerceinteraction.util.view.SlideSwitch;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.ViewById;

import java.util.Date;

@EActivity(R.layout.activity_creat_chat_room)
public class CreatChatRoomActivity extends BaseFragmentActivity implements SlideSwitch.SlideListener {

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @ViewById(R.id.ss_chatroom_switch)
    SlideSwitch chatRoomSwitch;

    @ViewById(R.id.ss_invite_member_switch)
    SlideSwitch inviteSwitch;

    @ViewById(R.id.et_charRoom_name)
    EditText chatRoomName;
    @ViewById(R.id.et_charRoom_announcement)
    EditText announcement;
    @ViewById(R.id.et_charRoom_Introduction)
    EditText Introduction;


    @Override
    @AfterViews
    void initView() {
        topTitle.setText(R.string.title_activity_creat_chat_room);

        setTranslucentStatus();
        memberIds = GlobalApplication.loginModel.getData().getUser_uniq_id();

        chatRoomSwitch.setSlideListener(this);
        inviteSwitch.setSlideListener(this);
    }

    @Override
    @Click({R.id.back_btn, R.id.tv_creat_btn, R.id.tv_creat_adduser_btn})
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.tv_creat_btn:
                creatGroupHttp();
                break;
            case R.id.tv_creat_adduser_btn:
                startToAddUser();
                break;
        }
    }

    @Override
    public void open(int viewId) {
        switch (viewId) {
            case R.id.ss_chatroom_switch:
                chatRoomPower = "1";
                break;
            case R.id.ss_invite_member_switch:
                invitePower = "1";
                break;
        }
    }

    @Override
    public void close(int viewId) {
        switch (viewId) {
            case R.id.ss_chatroom_switch:
                chatRoomPower = "2";
                break;
            case R.id.ss_invite_member_switch:
                invitePower = "2";
                break;
        }
    }

    String groupName;
    String notice;
    String descscription;
    String chatRoomPower = "1";//是否公开 1 是 2否
    String invitePower = "1";//是否允许群成员邀请别人加入此群 1是 2否
    String memberIds = "";

    boolean checkSumbmitValidate() {
        if (chatRoomName.getText().toString().trim().equals("")) {
            MethedUtil.showToast(this, "给群取个名称吧");
            return false;
        } else if (Introduction.getText().toString().trim().equals("")) {
            MethedUtil.showToast(this, "介绍介绍这个群");
        } else {
            groupName = chatRoomName.getText().toString().trim();
            notice = announcement.getText().toString().trim();
            descscription = Introduction.getText().toString().trim();
        }
        return true;
    }

    void creatGroupHttp() {
        LogHelper.i("creatGroupHttp");
        if (!checkSumbmitValidate()) {
            return;
        }
        requestUrl = RequestAddr.creatGroup +
                "?groupname=" + groupName +
                "&notice=" + notice +
                "&descscription=" + descscription +
                "&is_public=" + chatRoomPower +
                "&is_allowinvites=" + invitePower +
                "&members=" + memberIds;
        GlobalApplication.xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                dealBaseData(o);

            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }
    public static int RES_FRILIST = 001;
    public static int REQ_FRILIST = 002;
    void startToAddUser() {
        if (!checkSumbmitValidate()) {
            return;
        }
        Intent intent = new Intent();
        intent.setClass(CreatChatRoomActivity.this, SelectUserActivity_.class);
        startActivityForResult(intent,REQ_FRILIST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQ_FRILIST && resultCode == RES_FRILIST && data != null) {
            memberIds = memberIds + data.getStringExtra("friendList");
            creatGroupHttp();
        }
    }

    public boolean dataChange = false;
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent intent = new Intent();
        intent.putExtra("dataChange", dataChange);
        setResult(002, intent);
    }
}
